﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Telnet UI
 * Version 1.00
 * 
 * Selectr Connection Definition Dialog Class
 *      Provides a dialog definition for selecting a pre-defined connection definition.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Windows.Forms;

using Adaptive.Foundation.Windows.Forms;

namespace Adaptive.Telnet.UI
{
    /// <summary>
    /// Provides a dialog definition for selecting a pre-defined connection definition.
    /// </summary>
    public partial class SelectConnectionDialog : ModalDialogBase
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Definition list.
        /// </summary>
        private ConnectionDefinitionCollection _definitionList;

        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public SelectConnectionDialog()
            : base()
        {
            InitializeComponent();

        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }

            _definitionList = null;
            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the reference to the list of connection definitions to be displayed.
        /// </summary>
        /// <value>
        /// A <see cref="ConnectionDefinitionCollection"/> instance containing the list.
        /// </value>
        public ConnectionDefinitionCollection Definitions
        {
            get { return _definitionList; }
            set { _definitionList = value; }
        }
        /// <summary>
        /// Gets the item selected by the user.
        /// </summary>
        /// <value>
        /// A <see cref="ConnectionDefinition"/> instance.
        /// </value>
        public ConnectionDefinition SelectedDefinition
        {
            get
            {
                if (ConnectionList.SelectedItems.Count == 0)
                    return null;
                else
                    return _definitionList[ConnectionList.SelectedItems[0].Index];
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the controls on the dialog.
        /// </summary>
        protected override void AssignEventHandlers()
        {
            OkButton.Click +=new EventHandler(HandleOkClick);
            CloseButton.Click +=new EventHandler(HandleCloseClick);
            ConnectionList.SelectedIndexChanged +=new EventHandler(HandleIndexChange);
        }
        /// <summary>
        /// Removes the event handlers for the controls on the dialog.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
            OkButton.Click -= new EventHandler(HandleOkClick);
            CloseButton.Click -= new EventHandler(HandleCloseClick);
            ConnectionList.SelectedIndexChanged -= new EventHandler(HandleIndexChange);
        }
        /// <summary>
        /// Sets the display properties for the dialog.
        /// </summary>
        protected override void SetFormDisplay()
        {
            OkButton.Enabled = (ConnectionList.SelectedItems.Count > 0);
        }
        /// <summary>
        /// Sets the initial content for the dialog.
        /// </summary>
        protected override void SetInitialContent()
        {
            ListViewItem newItem = null;

            if (_definitionList != null)
            {
                _definitionList.MoveFirst();
                while (!_definitionList.Eof)
                {
                    ConnectionDefinition item = _definitionList.Value;

                    newItem = ConnectionList.Items.Add(item.Name, 0);
                    newItem.SubItems.Add(item.Description);
                    if (item.RemoteEndPoint.DomainName.Length > 0)
                        newItem.SubItems.Add(item.RemoteEndPoint.DomainName);
                    else
                        newItem.SubItems.Add(item.RemoteEndPoint.Address.ToString());

                    newItem.SubItems.Add(item.RemoteEndPoint.Port.ToString());
                    _definitionList.MoveNext();
                }
                if (_definitionList.Count > 0)
                {
                    ConnectionList.Items[0].Selected = true;
                    OkButton.Enabled = true;
                }
                else
                    OkButton.Enabled = false;
            }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        /// <summary>
        /// Handles the event when the OK button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleOkClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DialogResult = DialogResult.OK;
            Close();
        }
        /// <summary>
        /// Handles the event when the Cancel button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleCloseClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DialogResult = DialogResult.Cancel;
            Close();
        }
        /// <summary>
        /// Handles the event when the selection index changes.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleIndexChange(object sender, EventArgs e)
        {
            SetFormDisplay();
        }
        #endregion

    }
}
